package com.zlzlib.uiTest;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;


import com.zlzlib.libui.activity.BaseActivity;
import com.zlzlib.libui.util.AudioManagerHelper;
import com.zlzlib.libui.util.ViewClickDelay;
import com.zlzlib.uiTest.databinding.ActivityTestBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc:
 * @Copyright:
 * @DateTime: 2022/1/27 11:11
 * @Author zlz
 * @Version 1.0
 */
public class TestActivity extends BaseActivity<ActivityTestBinding> {

    //    private AudioManagerHelper audioManagerHelper;
    private final List<MediaPlayer> list = new ArrayList<>();

    @Override
    protected int getContentViewId() {
        return R.layout.activity_test;
    }

    @Override
    protected void initData() {

//        audioManagerHelper = new AudioManagerHelper(this);
//        audioManagerHelper.setMode(AudioManager.MODE_NORMAL);
//
//        audioManagerHelper.setAudioType(AudioManager.STREAM_VOICE_CALL).setVoice100(100);
//        audioManagerHelper.setAudioType(AudioManager.STREAM_MUSIC).setVoice100(80);
//
//        int micro =
//                audioManagerHelper.setAudioType(AudioManager.STREAM_VOICE_CALL).getSystemCurrentVolume();
//        int maxMicro = audioManagerHelper.setAudioType(AudioManager.STREAM_VOICE_CALL).getSystemMaxVolume();
//        int pro = micro * 100 / (maxMicro == 0 ? 1 : maxMicro);
//
//        int speaker =
//                audioManagerHelper.setAudioType(AudioManager.STREAM_MUSIC).getSystemCurrentVolume();
//        int maxSp = audioManagerHelper.setAudioType(AudioManager.STREAM_MUSIC).getSystemMaxVolume();
//        int proSp = speaker * 100 / (maxSp == 0 ? 1 : maxSp);
//        Log.i("TestActivity", "micro: " + micro + " -- maxMicro: " + maxMicro + " -- pro: "
//                + pro + " -- speaker: " + speaker + " -- maxSp: " + maxSp + " -- proSp: " + proSp);
        ViewClickDelay.addClick(mBinding.btn1, v -> {
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            list.add(startRinging(getBaseContext(), AudioManager.STREAM_MUSIC));
        });
        ViewClickDelay.addClick(mBinding.btn2, v -> {
            setVolumeControlStream(AudioManager.STREAM_RING);
            list.add(startRinging(getBaseContext(), AudioManager.STREAM_RING));
        });
        ViewClickDelay.addClick(mBinding.btn3, v -> {
            setVolumeControlStream(AudioManager.STREAM_ALARM);
            list.add(startRinging(getBaseContext(), AudioManager.STREAM_ALARM));
        });
        ViewClickDelay.addClick(mBinding.btn4, v -> {
            for (MediaPlayer ringPlayer : list) {
                if (ringPlayer != null && ringPlayer.isPlaying()) {
                    ringPlayer.stop();
                    ringPlayer.release();
                }
                ringPlayer = null;
            }
            list.clear();
        });
    }

    public static MediaPlayer startRinging(Context context, int streamType) {
        MediaPlayer player = new MediaPlayer();
        try {
            player.setAudioStreamType(streamType);
            AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.ring);
            player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
            player.setLooping(true);
            player.prepare();
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return player;
    }

    private static Uri getDevicePhoneRing(Context context) {
        return RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
    }

}
